/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.org.kasir.service.impl;

import java.util.ArrayList;
import com.org.kasir.db.ArrayReader;
import com.org.kasir.db.Db;
import com.org.kasir.service.BarangService;
import com.org.kasir.model.Barang;
import org.springframework.stereotype.Service;

/**
 *
 * @author Ivan
 */
@Service("barangService")
public class BarangServiceImpl implements BarangService {

    public Barang getDetailBarang(String kode) {
        Db db = null;
        Barang barang = new Barang();
        try {
            db = new Db(true);
            ArrayList temp = db.getData("select id,kode,nama,harga,stock from t_barang where kode='" + kode + "'");
            db.closer();
            if (temp.size() > 0) {
                barang.setKode(ArrayReader.getObjectFromArray(temp, 0, 1).toString());
                barang.setNama(ArrayReader.getObjectFromArray(temp, 0, 2).toString());
                barang.setHarga(Double.parseDouble(ArrayReader.getObjectFromArray(temp, 0, 3).toString()));
                barang.setStock(Integer.parseInt(ArrayReader.getObjectFromArray(temp, 0, 4).toString()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return barang;
    }

    public void save() {
        Db db = null;
        Barang barang = null;
        try {
            db = new Db(true);
            barang = new Barang();
            db.updateData("insert into t_barang(kode,nama,harga,stock)values('" + barang.getKode() + "','" + barang.getNama() + "','" + barang.getHarga() + "','" + barang.getStock() + "')");
            db.closer();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public ArrayList<Barang> getDaftarBarang() {
        Db db = null;
        ArrayList<Barang> list = new ArrayList<Barang>();
        try {
            db = new Db(true);
            ArrayList temp = db.getData("select id,kode,nama,harga,stock from t_barang");
            db.closer();
            if (temp.size() > 0) {
                for (int x = 0; x < temp.size(); x++) {
                    Barang barang = new Barang();
                    barang.setKode(ArrayReader.getObjectFromArray(temp, x, 1).toString());
                    barang.setNama(ArrayReader.getObjectFromArray(temp, x, 2).toString());
                    barang.setHarga(Double.parseDouble(ArrayReader.getObjectFromArray(temp, x, 3).toString()));
                    barang.setStock(Integer.parseInt(ArrayReader.getObjectFromArray(temp, x, 4).toString()));
                    list.add(barang);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return list;
    }

    public ArrayList<Barang> getCariBarang(String nama) {
        Db db = null;
        ArrayList<Barang> list = new ArrayList<Barang>();

        try {
            db = new Db(true);
            ArrayList temp = db.getData("select id,kode,nama,harga,stock from t_barang where nama LIKE '%" + nama + "%'");
            if (temp.size() > 0) {
                for (int x = 0; x < temp.size(); x++) {
                    Barang barang = new Barang();
                    barang.setKode(ArrayReader.getObjectFromArray(temp, x, 1).toString());
                    barang.setNama(ArrayReader.getObjectFromArray(temp, x, 2).toString());
                    barang.setHarga(Double.parseDouble(ArrayReader.getObjectFromArray(temp, x, 3).toString()));
                    barang.setStock(Integer.parseInt(ArrayReader.getObjectFromArray(temp, x, 4).toString()));
                    list.add(barang);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return list;
    }

    public void delete(String kode) {
        Db db = null;
        try {
            db = new Db(true);
            db.updateData("delete from t_barang where kode='" + kode + "'");
            db.closer();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void updateStock(Barang barang) {
        Db db = null;
        try {
            db = new Db(true);
            db.updateData("update t_barang set  nama='" + barang.getNama() + "', harga='" + barang.getHarga() + "',stock='" + barang.getStock() + "' where kode='" + barang.getKode() + "'");
            db.closer();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
